// pages/bill/bill.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    billYear: [],
    pal: 0,
    income: 0,
    show: false,
    year: '2021',
    actions: [
      {
        name: '2021',
      },
      {
        name: '2020',
      },
      {
        name: '2019',
      },
      {
        name: '2018',
      },
      {
        name: '2017',
      },
      {
        name: '2016',
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userId = getApp().globalData.userId
    this.getBill(2021, userId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getBill(year, userId) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8181/bill/getBillYear', //仅为示例，并非真实的接口地址
      data: {
        userId: userId,
        year: year
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res)
        that.setData({
          billYear: res.data
        })
        var bill = res.data
        var pal = 0;
        var income = 0;
        for (var i = 0; i < bill.length; i++) {
          pal = bill[i].pal + pal
          income = bill[i].income + income
        }
        that.setData({
          pal,
          income
        })
      }
    })
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  onSelect(event) {
    console.log(event.detail);
    var userId = getApp().globalData.userId
    this.setData({
      year: event.detail.name
    })
    this.getBill(event.detail.name, userId)
  },

})